package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shi
 * @version v1.0
 * @date 2019/8/7 21:37
 * @description TODO
 **/
@Service(interfaceClass = SystemSettingService.class)
@Transactional
public class SystemSettingServiceImpl implements SystemSettingService {


    @Autowired
    MenuDao menuDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        /**
         *  @描述：分页查询
         */
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> menus =  menuDao.findPage(queryString);
        return new PageResult(menus.getTotal(),menus.getResult());
    }

    @Override
    public List<Menu> findParentMenu() {
        return menuDao.findParentMenu();
    }

    @Override
    public void addMenu(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId==null||parentMenuId==0){
            menu.setLevel(1);
            List<Menu> parentMenu = menuDao.findParentMenu();
            menu.setPriority(parentMenu.size()+1);
        }else {
            menu.setLevel(2);
            List<Menu> childrenMenu = menuDao.findChildrenMenuByid(parentMenuId);
            menu.setLevel(childrenMenu.size()+1);
        }
        menuDao.addMenu(menu);
    }
}
